package com.example.callloggenerator;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.provider.CallLog;
import android.util.Log;
import android.widget.Toast;

import com.example.callloggenerator.CallLogUtils;

import java.util.Calendar;
import java.util.Random;
import java.util.concurrent.TimeUnit;

// 通话记录生成类

public class CallLogGenerator {
    private Context context;

    public CallLogGenerator(Context context) {
        this.context = context;
    }



    //单条通话记录生成
    public void generateCallLogs(MainActivity.CallLogData callData){
        String number = callData.getNumber();
        long duration = callData.getDuration();
        Calendar calendar = callData.getCalendar();
        int type = callData.getType();

        ContentValues values = createCallLogValues(number, duration, calendar.getTimeInMillis(), type);

//        ContentValues values = new ContentValues();
//        values.put(CallLog.Calls.NUMBER, number);
//        values.put(CallLog.Calls.DATE, calendar.getTimeInMillis());
//        values.put(CallLog.Calls.DURATION, duration);
//        values.put(CallLog.Calls.TYPE, type);


        // 插入到系统通话记录
        Uri uri = context.getContentResolver().insert(CallLog.Calls.CONTENT_URI, values);
        if (uri != null) {
            Toast.makeText(context, "创建成功！", Toast.LENGTH_SHORT).show();
            Log.d("CallLogGenerator", "成功创建通话记录：" + uri.toString());
        } else {
            Toast.makeText(context, "创建失败！请检查输入或重启", Toast.LENGTH_SHORT).show();
            Log.e("CallLogGenerator", "创建失败！");
        }

    }



    // 批量生成通话记录的方法
    public void batchGenerateCallLogs(MainActivity.CallLogData callLogData) {
        int count = 10;
        Calendar calendar = callLogData.getCalendar();
        //获取初始时间戳
        long startTime = calendar.getTimeInMillis();
        int type = callLogData.getType();
        for (int i = 0; i < count; i++) {
            // 生成随机数据
            String number = CallLogUtils.generateRandomPhoneNumber();
            long duration = getRandomCallDuration();
            ContentValues values = createCallLogValues(number, duration, startTime+i*getRandomTimeInterval(), type);

            // 设置默认SIM卡的订阅ID（仅适用于API 24及以上）
            setSubscriptionId(values);

            // 插入到系统通话记录
            Uri uri = context.getContentResolver().insert(CallLog.Calls.CONTENT_URI, values);
            if (uri != null) {
                Log.d("CallLogGenerator", "成功创建通话记录：" + uri.toString());
            } else {
                Log.e("CallLogGenerator", "创建失败！");
            }

            // 更新时间
            calendar.add(Calendar.MILLISECOND, (int) getRandomTimeInterval());
        }
    }

    // 批量生成400通话记录的方法
    public void batchGenerateCall400Logs(MainActivity.CallLogData callLogData) {
        int count = 10;
        Calendar calendar = callLogData.getCalendar();
        //获取初始时间戳
        long startTime = calendar.getTimeInMillis();
        int type = callLogData.getType();
        for (int i = 0; i < count; i++) {
            // 生成随机数据
            String number = CallLogUtils.generateRandom400PhoneNumber();
            long duration = getRandomCallDuration();
            ContentValues values = createCallLogValues(number, duration, startTime+i*getRandomTimeInterval(), type);

            // 设置默认SIM卡的订阅ID（仅适用于API 24及以上）
            setSubscriptionId(values);

            // 插入到系统通话记录
            Uri uri = context.getContentResolver().insert(CallLog.Calls.CONTENT_URI, values);
            if (uri != null) {
                Log.d("CallLogGenerator", "成功创建通话记录：" + uri.toString());
            } else {
                Log.e("CallLogGenerator", "创建失败！");
            }

            // 更新时间
            calendar.add(Calendar.MILLISECOND, (int) getRandomTimeInterval());
        }
    }


    // 随机生成时间间隔（2到5分钟）
    private long getRandomTimeInterval() {
        Random random = new Random();
        return TimeUnit.MINUTES.toMillis(3 + random.nextInt(3)); // 2 to 5 minutes
    }

    // 随机生成通话时长（60秒以内）
    private long getRandomCallDuration() {
        Random random = new Random();
        return random.nextInt(60); // 0 to 59 seconds
    }

    // 创建通话记录的ContentValues对象
    private ContentValues createCallLogValues(String number, long duration, long date, int type) {
        ContentValues values = new ContentValues();
        values.put(CallLog.Calls.NUMBER, number);
        values.put(CallLog.Calls.DATE, date);
        values.put(CallLog.Calls.DURATION, duration);
        values.put(CallLog.Calls.TYPE, type); // 或者使用其他类型
        values.put(CallLog.Calls.NEW, 1);
        return values;
    }

    // 设置默认SIM卡的订阅ID（仅适用于API 24及以上）
    private void setSubscriptionId(ContentValues values) {
        int subId = CallLogUtils.getDefaultSubscriptionId(context);
        if (subId != -1) {
            values.put(CallLog.Calls.PHONE_ACCOUNT_ID, subId);
        }
    }
}
